package com.cms.ffmpeg;

import android.util.Log;



public class VideoUtils {

    private static final String TAG = VideoUtils.class.getSimpleName();
    private static volatile VideoUtils instance;
    private static MyRxFFmpegSubscriber myRxFFmpegSubscriber;

    private static int index = 0;

    /**
     * 视频合成参数配置
     * 转码速度
     */
    private  String lowLevel = "ultrafast";
    private  String upLoadLowLevel = "ultrafast";

    private VideoUtils() {

    }

    public static VideoUtils getInstance() {
        if (instance == null) {
            synchronized (VideoUtils.class) {
                if (instance == null) {
                    instance = new VideoUtils();
                }
            }
        }
        return instance;
    }

    private String outPath = "";


    public interface OnRunListener {
        void onSuccess(String outPath);

        void onFailure();

        void onProgress(long progress);
    }


    /**
     * ffmpeg执行命令
     *
     * @auth sxy
     * @date 2022/11/4 15:37
     */
    public void execCmd(CmdList cmd, final OnRunListener onRunListener) {
        String[] cmds = cmd.toArray(new String[cmd.size()]);
        Log.i("VideoUtils", "mergeVideo: 执行命令 " + cmd.toString());

        myRxFFmpegSubscriber = new MyRxFFmpegSubscriber();
        myRxFFmpegSubscriber.setMyRxFFmpegSubscriber(new MyRxFFmpegSubscriber.FFmpegListener() {
                                                         @Override
                                                         public void success() {
                                                             onRunListener.onSuccess(outPath);
                                                         }

                                                         @Override
                                                         public void onProgress(int progress, long progressTime) {
                                                             Log.i(TAG, "1onProgress: " + progress + "-<" + progressTime);
                                                             onRunListener.onProgress(progressTime);
                                                         }

                                                         @Override
                                                         public void onCancel() {
                                                         }

                                                         @Override
                                                         public void onError(String message) {
                                                             Log.i("TAGcrop", "onFail,onFailure1: 视频合成:onError" + message);
                                                             onRunListener.onFailure();
                                                         }
                                                     }
        );

        try {
            RxFFmpegInvoke.getInstance()
                    .runCommandRxJava(cmds)
                    .subscribe(myRxFFmpegSubscriber);
        } catch (Exception e) {
            onRunListener.onFailure();
        }


    }


    public void release() {
        if (myRxFFmpegSubscriber != null) {
            myRxFFmpegSubscriber.dispose();
        }
    }

}
